﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using BUS;
using DAO;

namespace GUI
{
    public partial class usTongCongSuat : UserControl
    {
        public usTongCongSuat()
        {
            InitializeComponent();
        }
        private void btnchonPin_Click(object sender, EventArgs e)
        {
            frmChon frm = new frmChon(lbFinal.Text);
            // frm.MdiParent = this;
            frm.ShowDialog();
        }
        private void tshoTroTimKiem_Click(object sender, EventArgs e)
        {
            if (pntimkiem.Visible == true)
                pntimkiem.Visible = false;
            else
                pntimkiem.Visible = true;
        }
        private void tsSuaCS_Click(object sender, EventArgs e)
        {
            TrangThai(false);
        }
        private void tsthemCS_Click(object sender, EventArgs e)
        {
            TrangThai(false);
        }

        private void gvnhapSP_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
        {
            e.Row.Cells["clTB_STT"].Value = (int)-1;
            e.Row.Cells["clSL"].Value = (int)1;
            e.Row.Cells["clTG"].Value = (float)1;
            e.Row.Cells["clHH"].Value = (float)0;
            e.Row.Cells["clDB"].Value = (float)1;
            e.Row.Cells["clCS"].Value = (float)0;
            e.Row.Cells["clTong"].Value = (int)0;
        }
        private void gvnhapSP_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (gvnhapSP.Rows.Count > 1)
            {
                try
                {
                    int sl = int.Parse(gvnhapSP.CurrentRow.Cells["clSL"].Value.ToString());
                    int cs = int.Parse(gvnhapSP.CurrentRow.Cells["clCS"].Value.ToString());
                    float hsdb = float.Parse(gvnhapSP.CurrentRow.Cells["clDB"].Value.ToString());
                    float hh = float.Parse(gvnhapSP.CurrentRow.Cells["clHH"].Value.ToString());
                    float tg = float.Parse(gvnhapSP.CurrentRow.Cells["clTG"].Value.ToString());

                    gvnhapSP.CurrentRow.Cells["clTong"].Value = ((cs * hsdb) + hh) * tg * sl;

                    decimal _Tong = 0;
                    int n = gvnhapSP.Rows.Count - 1;
                    for (int i = 0; i < n; i++)
                    {
                        _Tong += decimal.Parse(gvnhapSP.Rows[i].Cells["clTong"].Value.ToString());
                    }
                    lbTong.Text = _Tong.ToString();
                    if (_Tong == 0)
                        btnchonPin.Enabled = false;
                    else
                        btnchonPin.Enabled = true;
                }
                catch
                {
                    gvnhapSP.CurrentCell.Value = 0;
                }
            }
        }
        private void gvnhapSP_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            decimal _gt = decimal.Parse(gvnhapSP.CurrentRow.Cells[6].Value.ToString());
            lbTong.Text = (decimal.Parse(lbTong.Text) - _gt).ToString();
        }

        private void TrangThai(bool status)
        {
            gvnhapSP.ReadOnly = status;
            gvnhapSP.AllowUserToAddRows = !status;
            gvnhapSP.AllowUserToDeleteRows = !status;

        }

        private void gvnhapSP_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            TextBox a = e.Control as TextBox;
            a.Name = "";

            if (gvnhapSP.CurrentCellAddress.X == gvnhapSP.CurrentCell.ColumnIndex && gvnhapSP.CurrentCell.ColumnIndex != 0)//gvnhapSP.Columns[3].DisplayIndex)
            {
                if (a != null)
                {
                    a.Name = "txt";
                    a.KeyPress += new KeyPressEventHandler(a_KeyPress);

                }
            }
        }

        void a_KeyPress(object sender, KeyPressEventArgs e)
        {

        }

        private void gvTimkiem_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (gvTimkiem.Rows.Count < 1)
                    return;
                TrangThai(false);
                DataGridViewRow _newRow = (DataGridViewRow)gvnhapSP.Rows[0].Clone();
                _newRow.Cells[0].Value = gvTimkiem.CurrentRow.Cells["clTen"].Value;
                _newRow.Cells[1].Value = gvTimkiem.CurrentRow.Cells["clTenLoai"].Value;
                _newRow.Cells[2].Value = 1;
                _newRow.Cells[3].Value = gvTimkiem.CurrentRow.Cells["clCongSuat"].Value;
                _newRow.Cells[4].Value = gvTimkiem.CurrentRow.Cells["clCSHH"].Value;
                _newRow.Cells[5].Value = gvTimkiem.CurrentRow.Cells["clHSDB"].Value;
                _newRow.Cells[6].Value = 1;
                _newRow.Cells[8].Value = gvTimkiem.CurrentRow.Cells["clSTT"].Value;
                _newRow.Cells[9].Value = gvTimkiem.CurrentRow.Cells["clMaTB"].Value;
                for (int i = 0; i < gvnhapSP.Rows.Count-1; i++)
                {
                    if (gvnhapSP.Rows[i].Cells["clTB_STT"].Value.ToString() == _newRow.Cells[8].Value.ToString())
                    {
                        MessageBox.Show("Đã tồn tại sản phẩm trong giỏ hàng!");
                        gvnhapSP.Rows[i].Selected = true;
                        gvTimkiem.Rows.Remove(gvTimkiem.CurrentRow);
                        return;
                    }
                }
                gvnhapSP.Rows.Insert(gvnhapSP.Rows.Count - 1, _newRow);
                gvTimkiem.Rows.Remove(gvTimkiem.CurrentRow);

                int sl = int.Parse(gvnhapSP.Rows[gvnhapSP.Rows.Count - 2].Cells["clSL"].Value.ToString());
                int cs = int.Parse(gvnhapSP.Rows[gvnhapSP.Rows.Count - 2].Cells["clCS"].Value.ToString());
                float hsdb = float.Parse(gvnhapSP.Rows[gvnhapSP.Rows.Count - 2].Cells["clDB"].Value.ToString());
                float hh = float.Parse(gvnhapSP.Rows[gvnhapSP.Rows.Count - 2].Cells["clHH"].Value.ToString());
                float tg = float.Parse(gvnhapSP.Rows[gvnhapSP.Rows.Count - 2].Cells["clTG"].Value.ToString());

                gvnhapSP.Rows[gvnhapSP.Rows.Count - 2].Cells["clTong"].Value = ((cs * hsdb) + hh) * tg * sl;
            }
            catch (Exception)
            {
            }
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            DataTable _tb= Search_NC_ThietBiDien();
            gvTimkiem.DataSource = _tb;
        }

        private DataTable Search_NC_ThietBiDien()
        {
            string sql = "select STT,MaTB,TenTB,CSKhaDung,TenLoai,CSHaoHut,HSDeBa from THIETBIDIEN a,LOAITBDIEN b where a.MaLoai=b.MaLoai ";
            if (!String.IsNullOrEmpty(txtMaTB.Text))
                sql += "AND MaTB ='" + txtMaTB.Text + "'";
            if (!String.IsNullOrEmpty(txtTenTB.Text))
                sql += "AND TenTB LIKE '%" + txtTenTB.Text + "%'";
            if (!String.IsNullOrEmpty(txtCSMin.Text))
                sql += "AND CSKhaDung >= " + txtCSMin.Text;
            if (!String.IsNullOrEmpty(txtCSMax.Text))
                sql += "AND CSKhaDung <= " + txtCSMax.Text;
            return Provider.TimKiem(sql);
        }
        private int PhuongThuc(int cs,int phuongthuc)
        {
            int _csTam = (int)((cs / float.Parse(txtNgayDuTru.Text))+1);
            if (phuongthuc == 2)
                _csTam += cs;
            return _csTam;
        }

        private void cbbPhuongThuc_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbPhuongThuc.SelectedIndex != 0)
            {
                lbNgay.Visible = true;
                txtNgayDuTru.Visible = true;
                txtNgayDuTru_TextChanged(null, null);
            }
            else
            {
                txtNgayDuTru.Text = "1";
                lbNgay.Visible = false;
                txtNgayDuTru.Visible = false;
            }
        }
        void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            char decimalChar = Convert.ToChar(decimalString);
            TextBox a = sender as TextBox;
            if (Char.IsDigit(e.KeyChar) || Char.IsControl(e.KeyChar)) { }
            else if (e.KeyChar == decimalChar && a.Text.IndexOf(decimalString) == -1)
            { }
            else
            {
                e.Handled = true;
            }
        }

        private void txtNgayDuTru_TextChanged(object sender, EventArgs e)
        {
            txtNgayDuTru_MouseLeave(null, null);
            lbFinal.Text=(PhuongThuc(int.Parse(lbTong.Text),cbbPhuongThuc.SelectedIndex)).ToString();
        }

        private void txtNgayDuTru_MouseLeave(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtNgayDuTru.Text))
            {
                txtNgayDuTru.Text = "1";
            }
        }

        private void lbFinal_TextChanged(object sender, EventArgs e)
        {
            if (int.Parse(lbFinal.Text) > 1)
                    btnchonPin.Enabled = true;
        }

        private void gvnhapSP_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == gvnhapSP.Columns["clXoa"].Index && e.RowIndex >= 0)
            {
                try
                {
                    decimal _gt = decimal.Parse(gvnhapSP.CurrentRow.Cells["clTong"].Value.ToString());
                    lbTong.Text = (decimal.Parse(lbTong.Text) - _gt).ToString();
                    gvnhapSP.Rows.Remove(gvnhapSP.CurrentRow);

                }
                catch { }
            }
        }

        private void btnhuy_Click(object sender, EventArgs e)
        {
            gvnhapSP.Rows.Clear();
        }
        private void SetupDataGridView(DataGridView gv)
        {
            DataGridViewCellStyle style = gv.ColumnHeadersDefaultCellStyle;
            style.BackColor = Color.Navy;
            style.ForeColor = Color.White;
            style.Font = new Font(gv.Font, FontStyle.Bold);

            gv.GridColor = Color.Blue;  //' Màu các của lưới
            gv.BackgroundColor = Color.AliceBlue; //' Màu nền gridview
            gv.ForeColor = Color.Green; //' Màu chữ
            //' Thiết lập màu chữ cho những dòng có chỉ số lẻ
            gv.AlternatingRowsDefaultCellStyle.ForeColor = Color.Purple;
        }

        private void usTongCongSuat_Load(object sender, EventArgs e)
        {
            SetupDataGridView(gvnhapSP);
            SetupDataGridView(gvTimkiem);
        }
    }
}
